package edu.tgc.service.gard;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.abyss.common.service.CommonCrudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import edu.tgc.entity.gard.Door;
import edu.tgc.entity.hrm.IdCade;
import edu.tgc.entity.student.TbStudent;
import edu.tgc.entity.system.SysUser;
import edu.tgc.repository.gard.DoorRepository;
import edu.tgc.repository.hrm.IdCadeRepository;
import edu.tgc.repository.student.TbStudentRepository;
import edu.tgc.repository.system.SysUserRepository;
@Service
public class DoorService extends CommonCrudService<Door, UUID> {
	
	@Autowired
	private DoorRepository doorDAO;
	@Autowired
	private SysUserRepository userDAO;
	@Autowired
	private TbStudentRepository studentDAO;
	@Autowired
	private IdCadeRepository idCadeDAO;
	
	public HashMap<String, Object> login(String idNo, Boolean auto, HttpServletRequest request, HttpServletResponse response) throws IOException {
		IdCade idCade = idCadeDAO.findByIdNo(idNo);
		HashMap<String, Object> result=new HashMap<>();
		if(idCade==null){
			result.put("success", false);
			result.put("msg", "没有此人的身份证记录");
		}else{
			SysUser user = userDAO.findByCade(idCade);
			if(user!=null){
				if(user.getEnabled()){
					Door door=new Door();
					door.setIdNo(user.getCade().getIdNo());
					door.setName(user.getCade().getName());
					door.setSex(user.getCade().getSex());
					door.setNation(user.getCade().getNation());
					door.setDept(user.getDept().getName());
					door.setStatus(user.getStatus().getName());
					door.setCreateTime(new Date());
					doorDAO.save(door);
					result.put("success", true);
					result.put("door", door);
				}else{
					result.put("success", false);
					result.put("msg", "此人已不是本校的在职人员");
				}
			}else{
				TbStudent student=studentDAO.findByCade(idCade);
				if(student==null){
					result.put("success", false);
					result.put("msg", "此人没有在学校注册");
				}else{
					if(student.getIsUsed()==1){
						Door door=new Door();
						door.setIdNo(idCade.getIdNo());
						door.setName(idCade.getName());
						door.setSex(idCade.getSex());
						door.setNation(idCade.getNation());
						door.setDept(student.getBanji().getParentdept().getName()+student.getBanji().getName());
						door.setStatus(student.getStatus().getName());
						door.setCreateTime(new Date());
						doorDAO.save(door);
						result.put("success", true);
						result.put("door", door);
						udpSend("on1:01");
					}else{
						result.put("success", false);
						result.put("msg","此人已毕业或离校");
					}
				}
			}
		}
		return result;
	}
	
	public void udpSend(String str) throws IOException{
		DatagramSocket  server = new DatagramSocket(5000);
       /* byte[] recvBuf = new byte[100];
        DatagramPacket recvPacket 
            = new DatagramPacket(recvBuf , recvBuf.length);
        server.receive(recvPacket);*/
        //String recvStr = new String(recvPacket.getData() , 0 , recvPacket.getLength());
        //System.out.println(str + recvStr);
        int port = 5000;
        InetAddress addr = InetAddress.getByName("192.168.1.42");
        String sendStr = str;
        byte[] sendBuf;
        sendBuf = sendStr.getBytes();
        DatagramPacket sendPacket 
            = new DatagramPacket(sendBuf , sendBuf.length , addr , port );
        server.send(sendPacket);
        server.close();
	}

}
